package com.scalar.forecast.salesForecastEntry.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.salesForecastEntry.forms.SalesForecastEntryForm;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.forecastReports.dao.ForecastReportsDAO;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.ProjectSetup;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * User: suppala
 * Date: Jun 15, 2010
 * Time: 10:29:20 PM
 */
public class SalesForecastWaterfallChartCommand extends ActionCommand {
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {
        SalesForecastEntryForm forecastEntryForm =  ( SalesForecastEntryForm ) form;
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute( SessionKeys.USER_KEY );
        LinkedHashMap errors = new LinkedHashMap();
        HashMap info = new HashMap();
        request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        String action = request.getParameter("action");
        if("loadWaterfallXHMTL".equalsIgnoreCase(action)) {

            ArrayList monthsHeadings = new ArrayList();
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, -6);
            int count = 0;
            int noOfMonths = 18;
            while(count < noOfMonths) {
                String heading = DateUtil.getMonthsArray()[cal.get(Calendar.MONTH)] + "-" + cal.get(Calendar.YEAR);
                monthsHeadings.add(heading);
                cal.add(Calendar.MONTH, 1);
                count++;
            }
            request.setAttribute("monthsHeadings", monthsHeadings);

            int projid = Integer.parseInt(request.getParameter("projId"));
            int prodid = Integer.parseInt(request.getParameter("prodId"));
            ProjectSetup projectSetup = new ProjectSetup();
            projectSetup.setProjectID(projid);
            ProjectSetupDAO.loadProjectSetupDetails(null, projectSetup);

            String customername = CustomerDAO.getCustomerName(null, projectSetup.getCustomerID());
            String partNumber = ProductDao.getBaseProductName(prodid+"");
            request.setAttribute("Customer",customername);
            request.setAttribute("Part_Number",partNumber);
            request.setAttribute("WaterFallList", ForecastReportsDAO.getSalesForecastWaterFallChart(projid, prodid));

            return mapping.findForward("waterfallXHMTL");
        }



        return null;
    }
}
